package com.people.core.validator.config;

import com.people.core.validator.factory.ValidatorFailFastFactory;
import com.people.core.validator.properties.ValidatorProperties;
import com.people.core.validator.util.ValidatorUtils;
import org.hibernate.validator.HibernateValidator;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;

/**
 * 验证器配置
 *
 * @author yuanqinglong
 * @date 2022/4/14 13:33
 */
@Configuration
@Import(value = {ValidatorUtils.class})
@EnableConfigurationProperties(ValidatorProperties.class)
public class ValidatorConfiguration {


    /**
     * 注册快速失败校验器
     *
     * @return ValidatorFailFastFactory
     */
    @Bean
    public ValidatorFailFastFactory validatorFailFast() {
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .addProperty("hibernate.validator.fail_fast", "true")
                .buildValidatorFactory();
        return new ValidatorFailFastFactory(validatorFactory);
    }

}
